package com.zml.utils;


import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 文件操作工具类
 * @Author <a href="hw86xll@163.com">Wei.Huang</a>
 * @Date 2012-9-13
 * @Version 1.0.0
 */
public class FileUtil {

    public static final String DIR_TMP = "/tmp/";

    public static final String DIR_FILES = "/files/";

    public static final String DIR_RRD = DIR_FILES + "rrd/";

    public static final String DEFAULE_ENCODING = "utf-8";

    public static File getIfAbsentCreate(String filePath) {
        File folder = new File(filePath);
        if (!folder.exists()) {
            folder.mkdirs();
        }
        return folder;
    }

    public static void closeOutputStrem(OutputStream out) {
        if (out == null)
            return;
        try {
            out.flush();
            out.close();
        } catch (Exception e) {
            // TODO: Ignore it
        }
    }

    public static void closeInputStrem(InputStream in) {
        if (in == null)
            return;
        try {
            in.close();
        } catch (Exception e) {
            // TODO: Ignore it
        }
    }

    public static String readFile(String fileName, String charset) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(FileUtil.class.
                getClassLoader().getResourceAsStream(fileName), charset));
        String line = null;
        StringBuilder text = new StringBuilder();
        while ((line = reader.readLine()) != null) {
            text.append(line).append("\n");

        }
        reader.close();
        return text.toString();
    }

    public static String readFile(String fileName) throws IOException {
        return readFile(fileName, DEFAULE_ENCODING);
    }

    public static boolean isDirectory(String path) {
        File dir = new File(path);
        return dir.isDirectory();
    }

    public static String getFileContent(String path) throws IOException {
        File tFile = new File(path);
        if (!tFile.isFile()) {
            throw new RuntimeException("不是文件");
        }
        RandomAccessFile file = new RandomAccessFile(tFile, "r");
        long fileSize = file.length();
        byte[] bytes = new byte[(int) fileSize];
        long readLength = 0L;
        while (readLength < fileSize) {
            int onceLength = file.read(bytes, (int) readLength, (int) (fileSize - readLength));
            if (onceLength > 0) {
                readLength += onceLength;
            }
            else {
                break;
            }
        }
        try {
            file.close();
        } catch (Exception e) {

        }
        return new String(bytes, DEFAULE_ENCODING);
    }

    /**
     *  获取当前路径下的文件清单
     *      isAddPathFlag=true的时候，返回实际路径+文件名
     *
     * @param path    需要截取的字符串
     * @param isAddPathFlag 要截取的长度
     */
    public static List<String> getDirectFiles(String path,boolean isAddPathFlag) {
        File file = new File(path);
        String[] fileInfo = file.list();
        if(fileInfo==null || fileInfo.length==0){
            throw new RuntimeException("当前目录下没有文件");
        }
        List<String> list = new ArrayList<String>();
        for(String fileName : fileInfo){
            if(FileUtil.isDirectory(path+"\\"+fileName)){
                list.addAll(FileUtil.getDirectFiles(file.getPath()+"\\"+fileName,isAddPathFlag));
            }else{
                list.add(isAddPathFlag==true?path+"\\"+fileName:fileName);
            }
        }
        return list;
    }
    public static void main(String[] args) throws IOException {
        System.out.println(FileUtil.getDirectFiles("d:\\fileTest",false));
        List<String> list =FileUtil.getDirectFiles("d:\\fileTest",false);
        for (String name :list){
            System.out.println(FileUtil.getFileContent(name));
            System.out.println("-------------------------");
        }
       // System.out.println(FileUtil.getFileContent(""));
    }
}
